﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Excel = Microsoft.Office.Interop.Excel;

namespace HexExplorer
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            addNew();
        }

        private void addNew()
        {
            this.listView.Items.Add(new DataBase() { From = 0, To = 0, Comment = "输入含义" });
        }

        private void removeSelected()
        {
            try
            {
                int selectedIndex = this.listView.SelectedIndex;
                this.listView.Items.RemoveAt(selectedIndex);
            }catch(Exception ex)
            {
                MessageBox.Show("请先选择一个条目，然后删除");
            }
        }

        private void add_Click(object sender, RoutedEventArgs e)
        {
            addNew();
        }

        private void remove_Click(object sender, RoutedEventArgs e)
        {
            removeSelected();
        }

        private void generator_Click(object sender, RoutedEventArgs e)
        {
            // 判断输入是否合法，并将其转换为没有空格的标准格式
            string inputData = this.dataInput.Text;
            if (string.IsNullOrEmpty(inputData))
            {
                MessageBox.Show("输入的值为空");
                return;
            }
            inputData = inputData.ToLower();
            inputData = inputData.Replace(" ", "");
            inputData = inputData.Replace("\n", "");
            inputData = inputData.Replace("\r", "");

            if (inputData.Length % 2 != 0)
            {
                MessageBox.Show("输入的数据不合法");
                return;
            }

            Excel.Application app = new Excel.Application();
            Excel.Workbook workbook = app.Workbooks.Add();
            Excel.Worksheet sheet = workbook.ActiveSheet;

            try
            {
                // 实现数据分割
                for (int i = 0; i < inputData.Length / 2; i++)
                {
                    sheet.Cells[2, (i + 1)] = inputData.Substring(i * 2, 2);
                    sheet.Cells[3, (i + 1)] = i;
                }

                for (int i = 0; i < this.listView.Items.Count; i++)
                {
                    DataBase item = this.listView.Items[i] as DataBase;
                    if (item == null)
                    {
                        MessageBox.Show("转换失败");
                        continue;
                    }
                    Excel.Range cell_1 = sheet.Cells[1, item.From + 1];
                    Excel.Range cell_2 = sheet.Cells[1, item.To + 1];
                    Excel.Range range = sheet.Range[cell_1, cell_2];
                    range.Merge();
                    range.Value = item.Comment;
                    range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                }

                app.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
